<?php
/**
 * Widget
 *
 * Classe de traitement du widget
 *
 * @author Valentin
 */
class Widget extends Object {
    protected $user;
    protected $params;
    protected $links;
    protected $wishes;

    function __construct($user_id) {
        $db = Database::getInstance();
        $this->user = new User($user_id);
        $sql = "SELECT COUNT(id) FROM widget
                WHERE id_user = '".$user_id."'";
        $result = $db->query($sql);
        if($result[0]['COUNT(id)'] == 0) {
            $this->createWidget($user_id);
        }
    }

    public function createWidget($user_id) {
        $db = Database::getInstance();
        $sql = "INSERT INTO widget
                VALUES(NULL,'".$user_id."',DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT,DEFAULT)";
        $db->query($sql);

        $this->params['linkColor'] = "#3366ff";
        $this->params['linkHover'] = "#3366ff";
        $this->params['textColor'] = "#000";
        $this->params['textActivButton'] = "#fff";
    }

    public function getParams() {
        if(isset($this->params)) return $this->params;
        $db = Database::getInstance();
        $this->params = array();
        $sql = "SELECT * FROM widget
                WHERE id_user = '".$this->user['id']."'";
        $results = $db->query($sql);
        foreach($results[0] as $key=>$line) {
            $this->params[$key] = $line;
        }
        return $this->params;
    }

    public function getWishes() {
        if(isset($this->wishes)) return $this->wishes;
        if(isset($this->params)) $this->getParams();
        if($this->params['showWishlist'] == 0) return;
        
        $db = Database::getInstance();
        $sql = "SELECT p.*, v.id as id_vendor, v.nom as nom_vendor
				FROM wishlists wl
                INNER JOIN produits p ON wl.produit_id = p.id
                INNER JOIN brands b ON p.brand_id = b.id
                INNER JOIN brands_vendors_link bvl ON bvl.brand_id = b.id
                INNER JOIN vendors v ON bvl.vendor_id = v.id
                WHERE wl.user_id = '".$this->user['id']."'";
        if($this->params['show3Wish'] == 0)
            $sql .= " AND wl.show_widget = 1";
        $sql .= " ORDER BY wl.added DESC";

        $result = $db->query($sql);
        $results = array();
        foreach($result as $line) {
            $results[] = new Product($line['id'],
                                      array(
                                        "nom" => $line['nom'],
                                        "vignette" => $line['vignette'],
                                        "prix" => $line['prix'],
                                        "vendor" => new Vendor($line['id_vendor'], array("nom" => $line['nom_vendor'])),
                                      )
                                    );
        }
        return $results;
    }

    public function getCss() {
        global $tpl;
        $tpl->assign('widget', $this);
        ob_start();
            $tpl->display("widget/css.tpl");
            $content = ob_get_contents();
        ob_end_clean();
        return addslashes(preg_replace("(\r\n|\n|\r)", "", $content));

    }
    public function getHtml() {
        global $tpl;
        $tpl->assign('widget', $this);
        ob_start();
            $tpl->display("widget/html.tpl");
            $content = ob_get_contents();
        ob_end_clean();
        return addslashes(preg_replace("(\r\n|\n|\r)", "", $content));
    }
    
    public function getAddUrl() {
        $chaine = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
        srand((double)microtime()*1000000);
        $a=array();
        for($i=0;$i<7;$i++){
            $tmp='';
            for($j=0;$j<rand(5,15);$j++) $tmp.= $chaine[rand()%strlen($chaine)];
            $a[$i]=$tmp;
        }
        return "c=".time()."&h=".$a[0]."&i=".$this->user['id']."&c=".$a[1]."&o=".$this->user['hash']."&f=1&r=".$a[4]."&a=".$a[2]."&m=".$a[5]."&p=".$a[6];
    }
    public function getTarget() {
        if(!isset($this->params)) $this->getParams();
        if($this->params['newWindow'] == 1) return "target=\"_blank\"";
        return "";
    }
    public function getUrlJs() {
        return URL_PATH."ecitizenWidget-".$this->user['id']."-".sha1($this->user['email'].$this->user['nom'].$this->user['prenom'].KEY_WIDGET).".js";
    }

    /*
     * Setters
     */
     public function setValue($key, $value) {
         $db = Database::getInstance();
         $sql = "UPDATE widget
                 SET ".$key." = '".$value."'
                 WHERE id_user = '".$this->user['id']."'";
         echo $sql;
         return $db->query($sql);
     }
     public function addProduct($id) {
         $db = Database::getInstance();
         $sql = "UPDATE wishlists
                 SET show_widget = '1'
                 WHERE user_id = '".$this->user['id']."' AND produit_id = '".$id."'";
         return $db->query($sql);
     }
     public function delProduct($id) {
         $db = Database::getInstance();
         $sql = "UPDATE wishlists
                 SET show_widget = '0'
                 WHERE user_id = '".$this->user['id']."' AND produit_id = '".$id."'";
         return $db->query($sql);
     }
}
?>
